package servlet.news;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jdbc.DBUtils;

@WebServlet("/NewsEditServlet")
public class NewsEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connection connection = DBUtils.getConn();
		try {
			 String id = request.getParameter("id");
			 id = new String(id.getBytes("iso-8859-1"),"utf-8");
			 
			 String title = request.getParameter("title");
			 title = new String(title.getBytes("iso-8859-1"),"utf-8");
			 
			 String content = request.getParameter("content");
			 content = new String(content.getBytes("iso-8859-1"),"utf-8");
			 
			 String linktype = request.getParameter("linktype");
			 linktype = new String(linktype.getBytes("iso-8859-1"),"utf-8");
			 
			 String linkurl = request.getParameter("linkurl");
			 linkurl = new String(linkurl.getBytes("iso-8859-1"),"utf-8");
			PreparedStatement ps = connection.prepareStatement("UPDATE news SET title=? , content=? , linktype=?,linkurl=? WHERE id=?");
			ps.setString(1, title);
			ps.setString(2, content);
			ps.setInt(3, Integer.valueOf(linktype));
			ps.setString(4, linkurl);
			ps.setInt(5, Integer.valueOf(id));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		response.sendRedirect("NewsListServlet");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}


